//
//  BBMineViewController.m
//  BlockBrowser
//
//  Created by Mac on 2018/4/3.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "BBMineViewController.h"
#import "BBSettingViewController.h"
#import "BBRuleModel.h"
#import "BBInviteViewController.h"
#import "BBReawardRecodViewController.h"
@interface BBMineViewController ()
@property(nonatomic, strong) NSArray                     *dataSouce;
@end

@implementation BBMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"个人中心";
    [self  configSubView];
    [self configDataSouce];
    // Do any additional setup after loading the view.
}
- (void)configDataSouce{
   
    BBRuleModel * rule1 = [BBRuleModel new];
    rule1.name = @"分享快讯将获得";
    rule1.unit = @"去分享";
    
    BBRuleModel * rule2 = [BBRuleModel new];
    rule2.name = @"邀请好友将获得";
    rule2.unit = @"立即邀请";
    
    NSMutableArray * dataSouce = [NSMutableArray new];
    [dataSouce addObject:rule1];
    [dataSouce addObjectsFromArray:[BBSystemManger manger].share_rule_news];
    [dataSouce addObject:rule2];
    [dataSouce addObjectsFromArray:[BBSystemManger manger].share_rule_qrcode];
    self.dataSouce = [dataSouce copy];
    [self.tableView reloadData];
}
#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth , 90)];
    header.backgroundColor = [BBThemeColorManger manger].backGroundColor;
    
    BBUserModel * user = [STUserManger defult].loginedUser;
    STButton * iconButton = [[STButton alloc] initWithFrame:CGRectMake(15, 15, 60, 60)
                                                     title:nil
                                                titleColor:nil
                                                 titleFont:0
                                              cornerRadius:30
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    [iconButton sd_setBackgroundImageWithURL:[NSURL URLWithString:user.avatar] forState:UIControlStateNormal];
    [header addSubview:iconButton];
    [iconButton showRoundShadow];
    
    iconButton.userInteractionEnabled = NO;
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(iconButton.right + 10, iconButton.top, 200, 20)
                                                     text:user.nickname
                                                textColor:FirstTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:titleLable];
    
    STLabel * idLable = [[STLabel alloc] initWithFrame:CGRectMake(iconButton.right + 10, titleLable.bottom  +5, 200, 20)
                                                  text:[NSString stringWithFormat:@"区块号:%@",
                                                        user.invitation_code.description]
                                                textColor:SecendTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:idLable];
    
    STButton * goButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 100, 44)
                                                     title:@"账号设置   "
                                                titleColor:SecendTextColor
                                                 titleFont:14
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:@"下一页(1)"]];
    goButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentCenter;
    [goButton sizeToFit];
    [goButton makeImageRight];
    goButton.right = UIScreenWidth - 15;
    goButton.centerY = 90 / 2;
    goButton.userInteractionEnabled = NO;
    [header addSubview:goButton];
    
    UIView * blueView = [[UIView alloc] initWithFrame:CGRectMake(0, 93, UIScreenWidth , 77)];
    blueView.backgroundColor = UIColorFromRGBA(0xE0EFF9);
    [header addSubview:blueView];
    if (BBThemeColorManger.isBlack) {
        blueView.backgroundColor = UIColor.clearColor;
    }
    UITapGestureRecognizer * tao = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelctedReawardButton)];
    [blueView addGestureRecognizer:tao];

    STLabel * diffcultLable = [[STLabel alloc] initWithFrame:CGRectMake(10, 10, UIScreenWidth/2 - 20, 50)
                                                     text:[NSString stringWithFormat:@"当前挖矿难度\n%@",
                                                           user.difficulty]
                                                textColor:TM_redColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [blueView addSubview:diffcultLable];
    
    
    
    STLabel * huoliLable = [[STLabel alloc] initWithFrame:CGRectMake(10, diffcultLable.bottom + 10, UIScreenWidth/2 - 20, 50)
                                                     text:[NSString stringWithFormat:@"%@挖矿获利次数\n%@",[BBSystemManger manger].candyName,user.count]
                                                textColor:SecendTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [blueView addSubview:huoliLable];
    
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(UIScreenWidth / 2, 10, 1, 57)];
    line.backgroundColor = [UIColor blackColor];
    line.backgroundColor = TM_lineColor;
    [blueView addSubview:line];
    
    STLabel * constLable = [[STLabel alloc] initWithFrame:CGRectMake(UIScreenWidth / 2, 10, UIScreenWidth/2 - 20, 50)
                                                     text:[NSString stringWithFormat:@"当前价值\nK%@",
                                                           user.cost]
                                                textColor:TM_redColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentRight];
    [blueView addSubview:constLable];
    
    
    STLabel * coshuLable = [[STLabel alloc] initWithFrame:CGRectMake(UIScreenWidth / 2,constLable.bottom +  10, UIScreenWidth/2 - 20, 50)
                                                     text:[NSString stringWithFormat:@"已获得%@糖果数量\n%@",[BBSystemManger manger].candyName,
                                                           user.coin]
                                                textColor:SecendTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentRight];
    [blueView addSubview:coshuLable];
    
    
    blueView.height = coshuLable.bottom + 10;
    line.height = blueView.height - 20;
    
    
    STLabel * desLable = [[STLabel alloc] initWithFrame:CGRectMake(15, blueView.bottom + 10, UIScreenWidth, 20)
                                                     text:[NSString stringWithFormat:@"如何挖矿获得%@糖果",[BBSystemManger manger].candyName]
                                                textColor:FirstTextColor
                                                     font:18
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:desLable];
    
    header.height = desLable.bottom + 20;
    self.tableView.tableHeaderView = header;
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectedSettingButton)];
    [header addGestureRecognizer:tap];
    
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count ;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    BBRuleModel * model = self.dataSouce[indexPath.row];
    cell.textLabel.text = model.name;
    if (!model.award) {
        model.award = @"";
    }
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@%@",model.award,model.unit];
    if ([model.name containsString:@"邀请好友将获得"]) {
        cell.detailTextLabel.text = @"";
        cell.textLabel.textColor = UIColor.blackColor;
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:16];
        cell.detailTextLabel.textColor = UIColor.blackColor;
        STButton * goButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 100, 44)
                                                        title:@"立即邀请   "
                                                   titleColor:UIColor.blackColor
                                                    titleFont:16
                                                 cornerRadius:0
                                              backgroundColor:nil
                                              backgroundImage:nil
                                                        image:[UIImage imageNamed:@"下一页(1)"]];
        goButton.contentHorizontalAlignment =UIControlContentHorizontalAlignmentCenter;
        [goButton sizeToFit];
        goButton.userInteractionEnabled = NO;
        [goButton makeImageRight];
        cell.accessoryView = goButton;
        cell.backgroundColor = UIColorFromRGBA(0xE0EFF9);
    }else{
        cell.accessoryView = nil;
        cell.textLabel.textColor = UIColorFromRGBA(0x333333);
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
        cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
        cell.backgroundColor = UIColor.whiteColor;
        if (BBThemeColorManger.isBlack) {
            cell.backgroundColor = [BBThemeColorManger manger].backGroundColor;
            cell.textLabel.textColor = UIColor.whiteColor;
        }
    }
    

    
    
    if (!model.award.length) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    }else{
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.detailTextLabel.textColor = FlatOrange;
        NSString * beike = model.unit;
        if ([beike containsString:@"MVG"]) {
            NSString * last = [beike componentsSeparatedByString:@"/"].lastObject;
            beike = [NSString stringWithFormat:@"🐚MVG/%@",last];
        }
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@%@",model.award,beike];
        cell.detailTextLabel.attributedText = [cell.detailTextLabel.text st_convertAttributeStringWithKeyWord:beike attributes:@{NSForegroundColorAttributeName: UIColorFromRGBA(0x999999)}];
    }


    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BBRuleModel * model = self.dataSouce[indexPath.row];
    if (!model.award.length && [model.name containsString:@"分享快讯"]) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    if (!model.award.length && [model.name containsString:@"邀请好友"]) {
        [self.navigationController pushViewController:[BBInviteViewController new] animated:YES];
    }
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelectedSettingButton{
    BBSettingViewController  * vc = [BBSettingViewController new];
    vc.userModel = [STUserManger defult].loginedUser;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)onSelctedReawardButton{
    [self.navigationController pushViewController:[BBReawardRecodViewController new] animated:YES];
}
@end
